package enterpriseCustomer

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"insighttracking/apps/admin/auth"
	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"
	"insighttracking/common/enum"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type CreateEnterpriseCustomerApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewCreateEnterpriseCustomerApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *CreateEnterpriseCustomerApiLogic {
	hostname, _ := os.Hostname()
	return &CreateEnterpriseCustomerApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *CreateEnterpriseCustomerApiLogic) CreateEnterpriseCustomerApi(req *types.CreateEnterpriseCustomerReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	if accountInfo.AccountType != enum.EnterpriseEnum {
		return "", errors.New("只有企业账号才可以创建门店")
	}
	_, err = service.EnterpriseCustomerService.GetByNameApi(l.ctx, accountInfo.EnterpriseId, req.Name)
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return "", errors.New(fmt.Sprintf("%s已经存在,不能重复", req.Name))
	}
	var fillAddress = ""
	if req.TownCode != "" {
		fillAddress, _ = service.AreaService.GetAreaNameApi(l.ctx, req.TownCode)
	} else {
		fillAddress, _ = service.AreaService.GetAreaNameApi(l.ctx, req.CountyCode)
	}
	tx := dao.Use(l.svcCtx.MysqlDb).Begin()
	enterpriseCustomerEntity := model.EnterpriseCustomerEntity{
		EnterpriseID: accountInfo.EnterpriseId,                       // 关联到enterprise表主键id
		Name:         req.Name,                                       // 门店名称
		Person:       req.Person,                                     // 负责人
		Mobile:       req.Mobile,                                     // 联系电话
		Logo:         req.Logo,                                       // 状态logo
		Status:       req.Status,                                     // 状态0：营业  1：停业
		Types:        req.Types,                                      // 客户类型0：公司，1：门店，2：个人
		ProvinceCode: req.ProvinceCode,                               // 省份id
		CityCode:     req.CityCode,                                   // 城市id
		CountyCode:   req.CountyCode,                                 // 县、区id
		TownCode:     req.TownCode,                                   // 乡镇code
		Address:      req.Address,                                    // 具体地址
		FullAddress:  fmt.Sprintf("%s/%s", fillAddress, req.Address), // 拼接省市县地址
	}
	if err = service.EnterpriseCustomerService.CreateApi(l.ctx, tx, &enterpriseCustomerEntity); err != nil {
		if err = tx.Rollback(); err != nil {
			return "", err
		}
		return "", err
	}
	// 2.员工账号信息
	//roleEntity, err := service.RoleService.GetByCodeApi(l.ctx, accountInfo.EnterpriseId, enum.RoleCode01Enum)
	//if err != nil {
	//	if err = tx.Rollback(); err != nil {
	//		return "", err
	//	}
	//	return "", errors.New("当前企业没有设置店长角色")
	//}
	//enterprisePersonEntity := model.EnterprisePersonEntity{
	//	EnterpriseID: accountInfo.EnterpriseId, // 关联到enterprise表主键id
	//	Name:         req.Name,                 // 员工姓名
	//	Mobile:       req.Mobile,               // 员工手机号码
	//	RoleID:       roleEntity.ID,            // 角色id,关联到role表主键id
	//}
	//if err = service.EnterprisePersonService.CreateApi(l.ctx, tx, &enterprisePersonEntity); err != nil {
	//	if err = tx.Rollback(); err != nil {
	//		return "", err
	//	}
	//	return "", err
	//}
	//// 3.创建账号
	//if err = service.AccountService.CreateApi(l.ctx, tx, &model.AccountEntity{
	//	Username:     req.Username,
	//	Password:     req.Password,
	//	EnterpriseID: accountInfo.EnterpriseId,
	//	ParentID:     accountInfo.AccountId,
	//	AccountType:  enum.EnterpriseCustomerEnum,
	//	CustomerID:   enterpriseCustomerEntity.ID,
	//	PersonID:     enterprisePersonEntity.ID,
	//}); err != nil {
	//	if err = tx.Rollback(); err != nil {
	//		return "", err
	//	}
	//	return "", err
	//}
	if err = tx.Commit(); err != nil {
		return "", errors.New("创建失败")
	}
	return "创建成功", nil
}
